package cmj.cloudmusic.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.adapter.TabFragmentAdapter;
import cmj.cloudmusic.fragment.MyLocalSongListFragment;
import cmj.cloudmusic.fragment.NONEfragment;

public class MyMuiscActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;

    MyLocalSongListFragment local;
    NONEfragment nonEfragment;
    NONEfragment nonEfragment2;
    NONEfragment nonEfragment3;

    List<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //设置状态栏透明 在setcontentView之前
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            //清除标记   透明状态栏 和  透明导航（华为控制按钮）
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(
                    //设置充满屏幕
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            //隐藏导航栏
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            );
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏透明
            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_my_muisc);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
                requestCameraPermission();
            }else {
                initView();
            }
        }



    }

    private void initView(){

        tabLayout= (TabLayout) findViewById(R.id.tablayout);

        viewPager= (ViewPager) findViewById(R.id.vp);

        List<String> titles=new ArrayList<>();

        titles.add("单曲");
        titles.add("歌手");
        titles.add("专辑");
        titles.add("文件夹");

        tabLayout.addTab(tabLayout.newTab().setText("单曲"),true);
        tabLayout.addTab(tabLayout.newTab().setText("歌手"));
        tabLayout.addTab(tabLayout.newTab().setText("专辑"));
        tabLayout.addTab(tabLayout.newTab().setText("文件夹"));

        local=new MyLocalSongListFragment();
        nonEfragment=new NONEfragment();
        nonEfragment2=new NONEfragment();
        nonEfragment3=new NONEfragment();

        fragments=new ArrayList<>();

        fragments.add(local);
        fragments.add(nonEfragment);
        fragments.add(nonEfragment2);
        fragments.add(nonEfragment3);

        TabFragmentAdapter adapter=new TabFragmentAdapter(getSupportFragmentManager(),fragments,titles);

        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
    }

    private static final int REQUEST_PERMISSION_STORAGE_CODE = 1;
    private void requestCameraPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_STORAGE_CODE) {
            int grantResult = grantResults[0];
            boolean granted = grantResult == PackageManager.PERMISSION_GRANTED;
            if (granted){
                Toast.makeText(this, "cmj", Toast.LENGTH_SHORT).show();
                initView();
            }

        }
    }
}
